package com.dangdi.show.shiro;

import com.dangdi.show.dao.ShiroDao;
import com.dangdi.show.service.ShiroService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

public class WechatUserRealm extends AuthorizingRealm {
    @Resource
    ShiroDao shiroDao;     //zhe li buyao yong service , yao yong mapper

    public String getName() {
        return "WechatUserRealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String open_id = authenticationToken.getPrincipal().toString();
        String wechatUser = shiroDao.FindWechatUserByOpenId(open_id);
        if (!wechatUser.equals(open_id)) {
            throw new UnknownAccountException();
        }
        AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(open_id, open_id, getName());
        return authenticationInfo;
    }
}
